/******************************************************************
 $ @version: 1.0
 $ @Author: zhl
 $ @Date: 2023-03-12 08:48:25
 $ @LastEditors: zhl
 $ @E-Mail: zhll168@163.com
 $ @Description: 业务描述
******************************************************************/
#ifndef _TCP_INTERFACE_H_
#define _TCP_INTERFACE_H_
#include <string.h>
#include <stdlib.h>
#include <memory>
#include "mongoose.h"
#include <unordered_map>
#include "CConfigManager.h"

using namespace std;
class TCPInterface {
public:
    struct mg_connection* c_conn;
    struct mg_mgr* c_mgr;
    std::string c_url;

private:
    static void event_callback(struct mg_connection *c, int ev, void *ev_data, void *fn_data);

    std::weak_ptr<CConfigManager> c_cm;

public:
    TCPInterface();
    ~TCPInterface();
    string get_net_url();

    std::string get_url(const char* ip, uint16_t port);
    int  init(std::weak_ptr<CConfigManager> cm, struct mg_mgr* mgr);
    int  connect(const char* url);
    int  connect(const char* ip, uint16_t port);
    int  send(const char *buf, int len);
    int  send(struct mg_connection* conn, const char *buf, int len);
    int  listen(const char* url);
    int  listen(const char* ip, uint16_t port);
    void close();
    void handle_msg(struct mg_connection* conn, const char* msg, int len);

};
#endif